---@class DistanceUtil
DistanceUtil = ClientFight.CreateClass("DistanceUtil")
local this = DistanceUtil
---是否在范围内
function this.isInRange(pos1, pos2, range)
    local dx = pos2.x - pos1.x;
    local dy = pos2.y - pos1.y;
    return dx * dx + dy * dy < range * range;
end
---计算距离的平方
function this.countDistanceSquared(pos1, pos2)
    return math.pow(pos1.x - pos2.x, 2) + math.pow(pos1.y - pos2.y, 2)
end
---计算距离
function this.countDistance(pos1, pos2)
    return math.sqrt(this.countDistanceSquared(pos1, pos2))
end
---计算平方
function this.squared(x)
    return math.pow(x, 2)
end
---矩形是否重叠
addOverload(this, "overlap", function(pos1, rec1, pos2, rec2)
    local length = math.floor((rec1.length + rec2.length) * Global.TEN_THOUSANDTH);
    local posX = math.floor((math.abs(pos1.x - pos2.x) * 2) * Global.TEN_THOUSANDTH);
    local width = math.floor((rec1.width + rec2.width) * Global.TEN_THOUSANDTH);
    local posY = math.floor((math.abs(pos1.y - pos2.y) * 2) * Global.TEN_THOUSANDTH);
    if (length >= posX and width >= posY) then
        return true;
    end
    return false;
end, "table", "table", "table", "table")
---圆形是否包含
addOverload(this, "overlap", function(pos1, radius, pos2)
    return this.countDistanceSquared(pos1, pos2) <= radius * radius
end, "table", "number", "table")

---* 原点与目标点 连线 偏移angle角度后 距离原点 len距离的 点
---param x number 原点x
---param y 原点y
---param tx 目标点x
---param ty 目标点y
---param len 原点到偏移点的距离
---param inderectAngle 偏移角度 ()
---param type 偏移类型 (0=逆时针,1=顺时针)
function this.calInderectPoint(x, y, tx, ty, len, inderectAngle, type)
    local angleSRC = 0;
    local dy = ty - y;
    local dx = tx - x;
    if (dx == 0) then
        if (dy == 0) then
            tx = x + 2
            return this.calInderectPoint(x, y, tx, ty, len, inderectAngle, type);
        elseif (dy > 0) then
            angleSRC = 90;
        else
            angleSRC = 270;
        end
    else
        local tana = dy * 1.0 / dx;
        local atana = math.atan(tana)
        angleSRC = math.deg(atana)
    end
    --第二和第三象限的 需要+180度,(atan算出来的是1,4)
    if (dx < 0) then
        angleSRC = angleSRC + 180
    end
    local angle = 0--计算最终的角度
    if (type == 0) then
        angle = angleSRC + inderectAngle
    else
        angle = angleSRC - inderectAngle
    end
    local tempx = 0;
    local tempy = 0;
    local tempAngle = angle % 360;
    if (tempAngle < 0) then
        tempAngle = tempAngle + 360;
    end

    if (tempAngle == 0 or tempAngle == 180) then
        tempy = 0;
    else
        tempy = (len * math.abs(math.sin(math.deg(tempAngle))));
        if (tempAngle > 180 and tempAngle < 360) then
            tempy = -tempy;
        end
    end
    if (tempAngle == 90 or tempAngle == 270) then
        tempx = 0;
    else
        tempx = (len * math.abs(math.cos(math.deg(tempAngle))));
        if (tempAngle > 90 and tempAngle < 270) then
            tempx = -tempx;
        end
    end
    return Vector2(x + tempx, y + tempy);
end
---获取两点的直线上的r长度的点
function this.getLinePoint(x1, y1, x2, y2, radius)
    local x3 = 0;
    local y3 = 0;
    local k = 0;
    local b = 0;

    if ((x2 - x1) == 0 and (y2 - y1 == 0)) then
        x2 = x1 + 2
        return this.getLinePoint(x1, y1, x2, y2, radius);
    end
    if ((x2 - x1) == 0) then
        if (y2 > y1) then
            x3 = x1;
            y3 = (y1 + radius);
            return Vector2(x3, y3);
        else
            x3 = x1;
            y3 = (y1 - radius);
            return Vector2(x3, y3);
        end
    end
    if ((y2 - y1) == 0) then
        if (x2 > x1) then
            y3 = y1;
            x3 = (x1 + radius);
            return Vector2(x3, y3);
        else
            y3 = y1;
            x3 = (x1 - radius);
            return Vector2(x3, y3);
        end
    end
    k = (y2 - y1) / (x2 - x1);
    if (k == 0 or k * k + 1 == 0) then
        return nil;
    end
    b = y1 - k * x1;
    local tmpV = math.sqrt(math.abs(((radius * radius - (y1 * y1 + b * b
            + x1 * x1 - 2 * y1 * b)) + (k * b - x1 - k * y1)
            * (k * b - x1 - k * y1) / (k * k + 1))
            / (k * k + 1)));
    local tmpV2 = (k * b - x1 - k * y1) / (k * k + 1);
    local tt = (tmpV - tmpV2);
    if ((x2 < x1 and tt > x1) or (x2 > x1 and tt < x1)) then
        tt = (-tmpV - tmpV2);
    end
    x3 = tt;
    y3 = tt * k + b;
    return Vector2(x3, y3);
end